package com.google.code.netbuilder.dal.providers.jpa;

import java.io.Serializable;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.google.code.netbuilder.dal.entities.Wire;
import com.google.code.netbuilder.dal.providers.IWireProvider;

/**
 * Class is JPA implementation of DAO for Wire Entity
 * Uses EntityManager from PersistenceContext
 * @author Sergey Kozlov
 * 
 *
 */
@Stateless
public class WireProviderJPA implements IWireProvider, Serializable{

	private static final long serialVersionUID = 8654412623948345287L;

	@PersistenceContext(unitName = "NetBuilder")
    EntityManager em;
	
	public WireProviderJPA() {
		
	}
	
	@Override
	public void save(Wire entity) throws RuntimeException {
		em.persist(entity);
	}

	@Override
	public void update(Wire entity) throws RuntimeException {
		em.merge(entity);
	}

	@Override
	public void delete(Wire entity) throws RuntimeException {
		em.refresh(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Wire> findAll() throws RuntimeException {
		Query q = em.createQuery("select w from Wire w");
		return (List<Wire>)q.getResultList();
	}

	@Override
	public Wire findByID(Integer id) throws RuntimeException {
		Query q = em.createQuery("select w from Wire w where w.id = :id").setParameter("id", id);
		return (Wire)q.getSingleResult();
	}

}
